﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyDMS.BusinessLayer;
using System.Reflection;
using System.IO;
using System.Data;

public partial class _Default : PageBaseClass
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<UserFolder> lstFolders = new UserFolderFactory().GetAllBy(UserFolder.UserFolderFields.User_Code, _author.user_Code);
        lsvFolders.DataSource = lstFolders;
        lsvFolders.DataBind();

        lsvProjects.DataSource = new UserInProjectFactory().GetAllBy(UserInProject.UserInProjectFields.User_Code, _author.user_Code);
        lsvProjects.DataBind();

        //List<UserDocumentInFolder> uDocInDefault;
        //foreach (UserFolder uFol in lstFolders)
        //{
        //    if (uFol.Is_Default)
        //    {
        //        uDocInDefault = new UserDocumentInFolderFactory().GetAllBy(UserDocumentInFolder.UserDocumentInFolderFields.Folder_Code, uFol.Folder_Code);
        //        break;
        //    }
        //}

        BindTheSD(_author.user_Code);
        BindTheUD(_author.user_Code);

        List<UserMessage> myMsgs = new UserMessageFactory().GetAllBy(UserMessage.UserMessageFields.User_Code, _author.user_Code);
        List<InboxMessage> inboxMsgs = new List<InboxMessage>();

        foreach (UserMessage m in myMsgs)
        {
            MailMessage mm = new MailMessageFactory().GetByPrimaryKey(new MailMessageKeys(m.Message_Code));
            InboxMessage im = new InboxMessage(m);
            im.Message_Subject = mm.Message_Subject;

            inboxMsgs.Add(im);
        }

        gvInbox.DataSource = inboxMsgs;
        gvInbox.DataBind();

    }

    private void BindTheSD(int userCode)
    {
        object dataObject = null;
        new SqlFactory().ExecuteQuery("MAN_Select_UserDocmentByUser " + userCode, out dataObject, SqlFactory.DataObjectType.DataSet);

        if (dataObject != null)
        {

            gvSharedDocuments.DataSource = dataObject;
            gvSharedDocuments.DataBind();
        }
    }

    private void BindTheUD(int userCode)
    {
        object dataObject = null;
        new SqlFactory().ExecuteQuery("MAN_Select_UploadedDocumentByOwner " + userCode, out dataObject, SqlFactory.DataObjectType.DataSet);

        if (dataObject != null)
        {

            gvUploadedDocs.DataSource = dataObject;
            gvUploadedDocs.DataBind();
        }
    }
    protected void gvSharedDocuments_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Download")
        {
            int index = Convert.ToInt16(e.CommandArgument);
            int documentCode = int.Parse(((GridView)e.CommandSource).Rows[index].Cells[1].Text);

            UploadedDocument upDoc = new UploadedDocumentFactory().GetByPrimaryKey(new UploadedDocumentKeys(documentCode));
            DownloadFile(upDoc.File_Path);
        }
    }

    protected void gvUploadedDocs_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Download")
        {
            int index = Convert.ToInt16(e.CommandArgument);
            int documentCode = int.Parse(((GridView)e.CommandSource).Rows[index].Cells[1].Text);

            UploadedDocument upDoc = new UploadedDocumentFactory().GetByPrimaryKey(new UploadedDocumentKeys(documentCode));
            DownloadFile(upDoc.File_Path);
        }
    }

    protected void btnLoadMessage_Click(object sender, EventArgs e)
    {
        MailMessage m = new MailMessageFactory().GetByPrimaryKey(new MailMessageKeys(int.Parse(hdfItemCode.Value)));
        lblFrm.Text = m.Owner.ToString();
        lblSubj.Text = m.Message_Subject;
        lblBody.Text = m.Message_Body;
    }

    private void DownloadFile(string filePath)
    {
        //http://dotnetslackers.com/Community/blogs/haissam/archive/2007/04/03/Downloading-Files-C_2300_.aspx

        // Get the physical Path of the file(test.doc)
        //string filepath = );

        // Create New instance of FileInfo class to get the properties of the file being downloaded
        FileInfo file = new FileInfo(filePath);

        // Checking if file exists
        if (file.Exists)
        {
            // Clear the content of the response
            Response.ClearContent();

            // LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header
            Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

            // Add the file size into the response header
            Response.AddHeader("Content-Length", file.Length.ToString());

            // Set the ContentType
            Response.ContentType = ReturnExtension(file.Extension.ToLower());

            // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
            Response.TransmitFile(file.FullName);

            // End the response
            Response.End();
        }
    }

    private string ReturnExtension(string fileExtension)
    {
        switch (fileExtension)
        {
            case ".htm":
            case ".html":
            case ".log":
                return "text/HTML";
            case ".txt":
                return "text/plain";
            case ".doc":
                return "application/ms-word";
            case ".tiff":
            case ".tif":
                return "image/tiff";
            case ".asf":
                return "video/x-ms-asf";
            case ".avi":
                return "video/avi";
            case ".zip":
                return "application/zip";
            case ".xls":
            case ".csv":
                return "application/vnd.ms-excel";
            case ".gif":
                return "image/gif";
            case ".jpg":
            case "jpeg":
                return "image/jpeg";
            case ".bmp":
                return "image/bmp";
            case ".wav":
                return "audio/wav";
            case ".mp3":
                return "audio/mpeg3";
            case ".mpg":
            case "mpeg":
                return "video/mpeg";
            case ".rtf":
                return "application/rtf";
            case ".asp":
                return "text/asp";
            case ".pdf":
                return "application/pdf";
            case ".fdf":
                return "application/vnd.fdf";
            case ".ppt":
                return "application/mspowerpoint";
            case ".dwg":
                return "image/vnd.dwg";
            case ".msg":
                return "application/msoutlook";
            case ".xml":
            case ".sdxl":
                return "application/xml";
            case ".xdp":
                return "application/vnd.adobe.xdp+xml";
            default:
                return "application/octet-stream";
        }
    }
}
